package com.ex.mzwqz.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.ex.mzwqz.fragment.BaseFragment;

import java.util.List;

/**
 * @author ExDog love_mobile@163.com
 * @ClassName: FragmentTabAdapter
 * @Description: 管理主界面四个fragment
 * @date 2014年7月8日 下午6:09:45
 */
public class FragmentTabAdapter implements RadioGroup.OnCheckedChangeListener {
    private List<String> titleList = null;
    private List<Fragment> fragments; // 一个tab页面对应一个Fragment
    private RadioGroup rgs; // 用于切换tab
    private FragmentActivity fragmentActivity; // Fragment所属的Activity
    private int fragmentContentId; // Activity中所要被替换的区域的id

    private int currentTab; // 当前Tab页面索引

    private OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener; // 用于让调用者在切换tab时候增加新的功能

    public FragmentTabAdapter(FragmentActivity fragmentActivity,
                              List<Fragment> fragments, int fragmentContentId, RadioGroup rgs) {
        this.fragments = fragments;
        this.rgs = rgs;
        for(int i = 0 ; i < fragments.size() ; i ++){
            String title = ((RadioButton)this.rgs.getChildAt(i)).getText().toString();
            ((BaseFragment)this.fragments.get(i)).setTitle(title);
        }
        this.fragmentActivity = fragmentActivity;
        this.fragmentContentId = fragmentContentId;
        toFirstPage();
        // 默认显示第一页
//        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager()
//                .beginTransaction();
//        ft.add(fragmentContentId, fragments.get(0));
//        ft.commit();

        rgs.setOnCheckedChangeListener(this);
    }


    public FragmentTabAdapter(FragmentActivity fragmentActivity,
                              List<Fragment> fragments, int fragmentContentId, RadioGroup rgs, List<String> titleList) {
        this.titleList = titleList;
        this.fragments = fragments;
        this.rgs = rgs;
        this.fragmentActivity = fragmentActivity;
        this.fragmentContentId = fragmentContentId;
        toFirstPage();
        // 默认显示第一页
//        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager()
//                .beginTransaction();
//        ft.add(fragmentContentId, fragments.get(0));
//        ft.commit();

        rgs.setOnCheckedChangeListener(this);
    }

    public void toFirstPage() {
        Fragment fragment = fragments.get(0);
        FragmentTransaction ft = obtainFragmentTransaction(0);

        getCurrentFragment().onPause(); // 暂停当前tab

        if (fragment.isAdded()) {
            fragment.onResume(); // 启动目标tab的onResume()
        } else {
            ft.add(fragmentContentId, fragment);
        }
        showTab(0); // 显示目标tab
        ft.commitAllowingStateLoss();
    }

    /*
     * public void toPage(int index){ Fragment fragment = fragments.get(index);
     * FragmentTransaction ft = obtainFragmentTransaction(index);
     *
     * //RadioButton vCurrent = (RadioButton) rgs.getChildAt(getCurrentTab());
     * // vCurrent.setChecked(false); getCurrentFragment().onPause(); // 暂停当前tab
     *
     * if(fragment.isAdded()){ fragment.onResume(); // 启动目标tab的onResume() }else{
     * ft.add(fragmentContentId, fragment); } // RadioButton v = (RadioButton)
     * rgs.getChildAt(index); // v.setChecked(true); //v.set showTab(index); //
     * 显示目标tab ft.commitAllowingStateLoss(); //ft.commit(); //
     * 如果设置了切换tab额外功能功能接口 if(null != onRgsExtraCheckedChangedListener){
     * onRgsExtraCheckedChangedListener.OnRgsExtraCheckedChanged(rgs,
     * rgs.getChildAt(index).getId(), index); } }
     */
    public void toPage(int index) {
        RadioButton v = (RadioButton) rgs.getChildAt(index);
        v.setChecked(true);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        for (int i = 0; i < rgs.getChildCount(); i++) {
            if (rgs.getChildAt(i).getId() == checkedId) {
                Fragment fragment = fragments.get(i);
                FragmentTransaction ft = obtainFragmentTransaction(i);

                getCurrentFragment().onPause(); // 暂停当前tab
                // getCurrentFragment().onStop(); // 暂停当前tab

                if (fragment.isAdded()) {
                    // fragment.onStart(); // 启动目标tab的onStart()
                    fragment.onResume(); // 启动目标tab的onResume()
                } else {
                    ft.add(fragmentContentId, fragment);
                }
                showTab(i); // 显示目标tab
                ft.commit();
                // 如果设置了切换tab额外功能功能接口
                if (null != onRgsExtraCheckedChangedListener) {
                    onRgsExtraCheckedChangedListener.OnRgsExtraCheckedChanged(
                            radioGroup, checkedId, i);
                }

            }
        }

    }

    /**
     * 切换tab
     *
     * @param idx
     */
    public void showTab(int idx) {
        for (int i = 0; i < fragments.size(); i++) {
            Fragment fragment = fragments.get(i);
            FragmentTransaction ft = obtainFragmentTransaction(idx);

            if (idx == i) {
                ft.show(fragment);
                ft.commit();
            } else {
                ft.hide(fragment);
                ft.commit();
            }
        }
        currentTab = idx; // 更新目标tab为当前tab
    }

    /**
     * 获取一个带动画的FragmentTransaction
     *
     * @param index
     * @return
     */
    private FragmentTransaction obtainFragmentTransaction(int index) {
        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager()
                .beginTransaction();
        // 设置切换动画
        /*
         * if(index > currentTab){ ft.setCustomAnimations(R.anim.slide_left_in,
		 * R.anim.slide_left_out); }else{
		 * ft.setCustomAnimations(R.anim.slide_right_in,
		 * R.anim.slide_right_out); }
		 */
        return ft;
    }

    public int getCurrentTab() {
        return currentTab;
    }

    public Fragment getCurrentFragment() {
        return fragments.get(currentTab);
    }

    public OnRgsExtraCheckedChangedListener getOnRgsExtraCheckedChangedListener() {
        return onRgsExtraCheckedChangedListener;
    }

    public void setOnRgsExtraCheckedChangedListener(
            OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener) {
        this.onRgsExtraCheckedChangedListener = onRgsExtraCheckedChangedListener;
    }

    /**
     * 切换tab额外功能功能接口
     */
    public static interface OnRgsExtraCheckedChangedListener {
        public void OnRgsExtraCheckedChanged(RadioGroup radioGroup,
                                             int checkedId, int index);
    }

}
